October 05, 2020
간단한 데이터를 저장하는 데 유용하다
주의!! 절대 데이터베이스로 쓰지 말 것
User Defaults data는 어디에 저장될까?
AppDelegate.swift
의 didFinishLaunchingWithOptions
func에 print 찍어보기
NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)
return true
}
→ /Users/[USER-NAME]/Library/Developer/CoreSimulator/Devices/141ED371-3CF8-43B8-B09F-702DF14D2912/data/Containers/Data/Application/1949A98D-B948-46BB-B6A1-64D225C4E2BE/Documents
위 루트에 plist가 위치해 있다.
Demo
import UIKit
let defaults = UserDefaults.standard
let dictionaryKey = "myDictionary"
defaults.set(0.24, forKey: "Volume")
defaults.set(true, forKey: "MusicOn")
defaults.set("Tori", forKey: "PlayerName")
defaults.set(Date(), forKey: "AppLastOpenedByUser")
let array = [1, 2, 3]
defaults.set(array, forKey: "myArray")
let dictionary = ["name": "Tori"]
defaults.set(dictionary, forKey: "myDictionary")
// later on
let volume = defaults.float(forKey: "Volume")
let appLastOpen = defaults.object(forKey: "AppLastOpenedByUser")
let myArray = defaults.array(forKey: "myArray") as! [Int]
let myDictionary = defaults.dictionary(forKey: dictionaryKey)
위 array, dictionary를 저장하는 예시는 좋은 practice가 아니다.
Reference
Apple Developer Documentation on UserDefaults